package com.huixue.cn.ztej.weixin.main;

import java.net.URLEncoder;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.huixue.cn.ztej.constants.Constant;
import com.huixue.cn.ztej.utils.Urls;
import com.huixue.cn.ztej.weixin.bean.AccessToken;
import com.huixue.cn.ztej.weixin.menu.Button;
import com.huixue.cn.ztej.weixin.menu.CommonButton;
import com.huixue.cn.ztej.weixin.menu.ComplexButton;
import com.huixue.cn.ztej.weixin.menu.Menu;
import com.huixue.cn.ztej.weixin.menu.UrlButton;
import com.huixue.cn.ztej.weixin.util.WeixinUtil;

public class MenuManager {
	private static Logger log = LoggerFactory.getLogger(MenuManager.class);
	public static void main(String[] args) {
        // 第三方用户唯一凭证
        String appId = Constant.AppID;
        // 第三方用户唯一凭证密钥
        String appSecret = Constant.AppSecret;
        // 调用接口获取access_token
        String  access_token_value = WeixinUtil.getAccessToken(appId, appSecret);
        //Urls.ACCESS_TOKEN = access_token_value;
        System.out.println(access_token_value);
        if (access_token_value!=null) {
            // 调用接口创建菜单
            int result = WeixinUtil.createMenu(getMenu(), access_token_value);

            // 判断菜单创建结果
            if (0 == result)
                log.info("菜单创建成功！");
            else
                log.info("菜单创建失败，错误码：" + result);
        }
    }
	
	
    /**
     * 组装菜单数据
     * 
     * @return
     */
    private static Menu getMenu() {
    	CommonButton btn11 = new CommonButton();
        btn11.setName("公司咨询");
        btn11.setType("click");
        btn11.setKey("gszx_11");

        CommonButton btn12 = new CommonButton();
        btn12.setName("操作流程");
        btn12.setType("click");
        btn12.setKey("czlc_12");

        CommonButton btn13 = new CommonButton();
        btn13.setName("平台说明");
        btn13.setType("click");
        btn13.setKey("ptsm_13");

     
        UrlButton btn21 = new UrlButton();
        btn21.setName("信息统计");
        btn21.setType("view");
        btn21.setUrl(Urls.transfromWeixinUrl(Urls.URL_INFO_STATISTICS));

        UrlButton btn22 = new UrlButton();
        btn22.setName("工作审批");
        btn22.setType("view");
        btn22.setUrl(Urls.transfromWeixinUrl(Urls.URL_APPROVE_LIST));

        UrlButton btn23 = new UrlButton();
        btn23.setName("零星机械申请");
        btn23.setType("view");
        btn23.setUrl(Urls.transfromWeixinUrl(Urls.URL_MECHANICA_APPLY));
        
        UrlButton btn24 = new UrlButton();
        btn24.setName("零星用工申请");
        btn24.setType("view");
        btn24.setUrl(Urls.transfromWeixinUrl(Urls.ULR_EMPLOY_APPLY));

        UrlButton btn31 = new UrlButton();
        btn31.setName("我的申请");
        btn31.setType("view");
        btn31.setUrl(Urls.transfromWeixinUrl(Urls.URL_MY_APPLY));

        UrlButton btn32 = new UrlButton();
        btn32.setName("我的信息");
        btn32.setType("view");
        btn32.setUrl(Urls.transfromWeixinUrl(Urls.URL_USERINOF));
      

        UrlButton btn33 = new UrlButton();
        btn33.setName("注册申请");
        btn33.setType("view");
        btn33.setUrl(Urls.transfromWeixinUrl(Urls.URL_REGISTER));

        
        /**
         * 微信：  mainBtn1,mainBtn2,mainBtn3底部的三个一级菜单。
         */
        
        ComplexButton mainBtn1 = new ComplexButton();
        mainBtn1.setName("关于平台");
        //一级下有4个子菜单
        mainBtn1.setSub_button(new Button[] { btn11, btn12, btn13 });

        
        ComplexButton mainBtn2 = new ComplexButton();
        mainBtn2.setName("业务中心");
        mainBtn2.setSub_button(new Button[] { btn21, btn22, btn23, btn24});

        
        ComplexButton mainBtn3 = new ComplexButton();
        mainBtn3.setName("个人中心");
        mainBtn3.setSub_button(new Button[] { btn31, btn32, btn33 });

        
        /**
         * 封装整个菜单
         */
        Menu menu = new Menu();
        menu.setButton(new Button[] { mainBtn1, mainBtn2, mainBtn3 });

        return menu;
    }
}
